package org.apache.ibatis.test;

import org.apache.ibatis.binding.MapperProxy;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.test.entity.ParamsVO;
import org.apache.ibatis.test.entity.Payment;
import org.apache.ibatis.test.mapper.PaymentMapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Description
 * @Author houchenkai
 * @Created Date: 2020/9/15 15:46
 * @ClassName
 */
public class OrgionCodeRead {

  public static void main(String[] args) throws IOException {
    //1、读取配置文件
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    //2、解析配置文件，并包保存到BaseBuilder中的configuration等变量中；用configuration去创建一个SqlSessionFactory
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //3、用SqlSessionFactory创建SqlSession会话
    SqlSession session = sqlSessionFactory.openSession();
    // MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
    // 创建PaymentMapper接口的invocation类实现类 并通过jdk动态代理创建代理对象
    PaymentMapper paymentMapper = session.getMapper(PaymentMapper.class);
    // 调用代理类的org.apache.ibatis.binding.MapperProxy.invoke 方法
    /**
     * @see MapperProxy#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
     */
    Payment payment = paymentMapper.getPayment(1);
    System.out.println(payment.toString());
  }
}
